﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class EmployeeManager
    {
        public EmployeeInfo GetEmployeeById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryEmployee = from d in db.Employees
                                    where d.Status.Equals(1)
                                    where d.Id.Equals(id)
                                    select new EmployeeInfo { Id = d.Id, Code = d.Code, Fullname = d.Fullname,Sex=d.Sex,Birthday=d.Birthday,Email=d.Email,Address=d.Address, Mobile=d.Mobile, IdentityCard=d.IdentityCard, DepartmentalId=d.DepartmentalId, NationalId=d.NationalId, ReligiousId=d.ReligiousId ,Status = d.Status};

                return queryEmployee.OrderBy(d => d.Code).FirstOrDefault();
            }

        }
        public Employee GetEmployeeByCode(string code)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryEmployee = from d in db.Employees
                                    where d.Status.Equals(1)
                                    where d.Code.Equals(code)
                                    select d;

                return queryEmployee.OrderBy(d => d.Code).FirstOrDefault();
            }

        }


        public IList<EmployeeInfo> ListEmployee()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryEmployee = from d in db.Employees
                                    where d.Status.Equals(1)
                                    select new EmployeeInfo { Id = d.Id, Code = d.Code, Fullname = d.Fullname, Sex = d.Sex, Birthday = d.Birthday, Email = d.Email, Address = d.Address, Mobile = d.Mobile, IdentityCard = d.IdentityCard, DepartmentalId = d.DepartmentalId, NationalId = d.NationalId, ReligiousId = d.ReligiousId, Status = d.Status };

                return queryEmployee.OrderBy(d => d.Code).ToList();
            }
        }

        public void CreateEmployee(Employee employee)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Employees.InsertOnSubmit(employee);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="employee"></param>
        public void UpdateEmployee(Employee employee)
        {
            using (var db = new QLKDSDataContext())
            {
                var _employee = db.Employees.Single(d => d.Id.Equals(employee.Id));

                _employee.Code = employee.Code;
                _employee.Fullname = employee.Fullname;
                _employee.Address = employee.Address;
                _employee.Mobile = employee.Mobile;
                _employee.Email = employee.Email;
                _employee.Status = employee.Status;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteEmployee(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _employee = db.Employees.Single(d => d.Id.Equals(id));

                _employee.Status = 0;

                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void RemoveEmployee(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _employee = db.Employees.Single(d => d.Id.Equals(id));

                db.Employees.DeleteOnSubmit(_employee);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool HasCodeExisted(string code)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Employees
                            where d.Code.Equals(code)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasCodeDuplicated(string code, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Employees
                            where d.Code.Equals(code)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public bool HasMobileExisted(string mobile)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Employees
                            where d.Mobile.Equals(mobile)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasMobileDuplicated(string mobile, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Employees
                            where d.Mobile.Equals(mobile)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool HasEmailExisted(string email)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Employees
                            where d.Email.Equals(email)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasEmailDuplicated(string email, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Employees
                            where d.Email.Equals(email)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }
    }
}
